package cn._2dland.comment
{
	import cn._2dland.comment.locator.CommentLocator;
	import cn._2dland.comment.locator.CommentLocatorFactory;
	import cn._2dland.ui.ResizableComponect;
	import cn._2dland.events.CommentEvent;
	import cn._2dland.utils.CommentUtils;
	
	import fl.transitions.Transition;
	
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.utils.getTimer;

	/**
	 * 评论舞台视图类 
	 * @author deadblue
	 */	
	public class CommentView extends ResizableComponect
	{
		private var _cachedTime:Number = -1;
		private var _timer:Timer = null;
		/** 弹幕定位器 */
		private var _locators:Object = null;

		public function CommentView(width:Number, height:Number) {
			backgroundAlpha = 0;
			_width = 540;
			_height = 384;
			resize();

			_timer = new Timer(CommentUtils.REFRESH_INTERVAL);
			_timer.addEventListener(TimerEvent.TIMER, timerHandler);

			_locators = {};
		}

		/**
		 * 开始播放评论
		 */
		public function play():void {
			_timer.start();
			_cachedTime = getTimer();
		}

		/**
		 * 暂停播放评论
		 */
		public function pause():void {
			_timer.stop();
		}

		/**
		 * 添加一条评论到舞台上
		 * @param cmntObj - 评论对象
		 */
		public function addComment(cmntObj:CommentObject):void {
			// 初始化评论组件
			var cmnt:CommentMC = new CommentMC();
			cmnt.setComment(cmntObj);
			
			// 监听效果完成事件
			cmnt.addEventListener(CommentEvent.EFFECT_END, effectEndHandler);

			// 计算定位
			var locator:CommentLocator = _locators[cmntObj.mode] as CommentLocator;
			if(locator == null) {
				locator = CommentLocatorFactory.createCommentLocator(cmntObj.mode);
				_locators[cmntObj.mode] = locator;
			}
			locator.locate(cmnt, this.rect);

			// 添加到舞台
			addChild(cmnt);
			cmnt.rotationZ = 30;
			cmnt.enter();
		}

		/**
		 * 移除指定的评论
		 */
		public function removeComment(cmnt:CommentMC):void {
			try{ removeChild(cmnt); }
			catch(err:Error){ trace(err); }
		}

		/**
		 * 移除所有评论
		 */
		public function removeAllComments():void {
			try {
				for(var i:int=numChildren; i>0; i--){
					removeChildAt(i-1);
				}
			}
			catch(err:Error) {
				trace(err);
			}
		}

		/**
		 * 获取视图区域
		 * @return 视图区域
		 */
		public function get rect():Rectangle{
			var rect:Rectangle = new Rectangle(this.x, this.y, this.width, this.height);
			return rect;
		}

		/**
		 * 计时器 响应函数
		 * @param e
		 */
		protected function timerHandler(e:TimerEvent):void {
			var currTime:Number = getTimer();
			var realInterval:Number = currTime - _cachedTime;
			_cachedTime = currTime;

			for(var i:int=0; i<this.numChildren; i++){
				var cmnt:CommentMC = this.getChildAt(i) as CommentMC;
				if(cmnt == null) continue;
				if(cmnt.effecting) continue;

				cmnt.x += cmnt.xSpeed * realInterval;
				cmnt.y += cmnt.ySpeed * realInterval;
				cmnt.life -= realInterval;
				
				if(cmnt.life <= 0){
					cmnt.leave();
				}
			}
		}

		/**
		 * 组件效果结束 响应函数
		 * @param e
		 */
		protected function effectEndHandler(e:CommentEvent):void {
			if(e.direction == Transition.OUT){
				// 移除事件响应函数
				e.target.removeEventListener(CommentEvent.EFFECT_END, effectEndHandler);
	
				var cmnt:CommentMC = e.target as CommentMC;
				if(cmnt == null) return;

				// 从定位器中移除
				var locator:CommentLocator = _locators[cmnt.mode] as CommentLocator;
				if(locator != null) {
					locator.remove(cmnt);
				}

				// 从舞台上移除
				removeComment(cmnt);
			}
		}
	}
}